package com.testape2d;

import java.util.ArrayList;
import java.util.Random;

//import org.jbox2d.dynamics.World;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.KeyEvent;
import android.view.MotionEvent;
//import android.util.Log;
import android.view.View;
import ape.APEngine;
import ape.AbstractParticle;
import ape.CircleParticle;
import ape.RectangleParticle;
import ape.SpringConstraint;
import ape.Vector;
import ape.WheelParticle;

public class PhysicsWorld extends View {
	   
//    protected static final int GUIUPDATEIDENTIFIER = 0x231;
//    public int targetFPS = 20;
	public int targetFPS = 40;
	public float timeStep = 22.0f / targetFPS;
/*  
  	public int iterations = 5;   
    public int World_W,World_H;
*/
    private Paint paint;

//	private long usedTime;

	private APEngine APEngine = new APEngine();
	private ArrayList paintQueue;
	private RectangleParticle rotatingRect;
	
//	private int t;
	
	private WheelParticle wheelParticleA;
	private WheelParticle wheelParticleB;
	
	float keySpeed = (float)0.2;

    public PhysicsWorld(Context context,int W,int H) {
         super(context);
      
       paint=new Paint();
//       paint.setStyle(Style.FILL);
       paint.setStyle(Style.STROKE);
       paint.setColor(Color.RED);
         
 		// set up the events, main loop handler, and the engine. you don't have to use
		// enterframe. you just need to call the ApeEngine.step() method wherever
		// and however your handling your program cycle.
			
 		// the argument here is the deltaTime value. Higher values result in faster simulations.
		APEngine.init((float)1/2);
		
		// SELECTIVE is better for dealing with lots of little particles colliding, 
		// as in the little rects and circles in this example
		APEngine.setCollisionResponseMode(APEngine.SELECTIVE);
		
		// gravity -- particles of varying masses are affected the same
		APEngine.addMasslessForce(new Vector((float)0,(float)3));
		
		// surfaces
		RectangleParticle floor = new RectangleParticle(paint,(float)W/2,(float)H-25-1,(float)W-1,(float)50,(float)0,true,(float)1,(float)0.3,(float)0);
		APEngine.addParticle(floor);
		
		RectangleParticle floorLeftAngle = new RectangleParticle(paint,(float)50,(float)390,(float)120,(float)20,(float)0.5,true,(float)1,(float)0.3,(float)0);
		APEngine.addParticle(floorLeftAngle);
		
		RectangleParticle leftWall = new RectangleParticle(paint,(float)14,(float)180,(float)30,(float)500,(float)0,true,(float)1,(float)0.3,(float)0);
		APEngine.addParticle(leftWall);
		
		RectangleParticle rightWall = new RectangleParticle(paint,(float)310,(float)180,(float)30,(float)500,(float)0,true,(float)1,(float)0.3,(float)0);
		APEngine.addParticle(rightWall);
		
		// rotator
		rotatingRect = new RectangleParticle(paint,(float)250,(float)280,(float)30,(float)14,(float)0,true,(float)1,(float)0.3,(float)0);
		APEngine.addParticle(rotatingRect);
		
		RectangleParticle littleRect = new RectangleParticle(paint,(float)250,(float)280,(float)10,(float)10,(float)0,false,(float)1,(float)0.3,(float)0);
		APEngine.addParticle(littleRect);
		
		SpringConstraint rotConnector = new SpringConstraint(paint,(AbstractParticle)rotatingRect.getCornerParticles().get(1), (AbstractParticle)littleRect, (float)0.2);
		APEngine.addConstraint(rotConnector);

		// little boxes
		for (int i = 0; i < 9; i++) {
			float px = (7 * i) + 60;
			APEngine.addParticle(new RectangleParticle(paint,px,(float)200,(float)10,(float)5,(float) (Math.random() * Math.PI),false,(float)1.8,(float)0.1,(float)0.0));
			APEngine.addParticle(new CircleParticle(paint,px+(float)40,(float)210,(float)3.5,false,(float)1.8,(float)0.1,(float)0.0));
		}
		
		paintQueue = APEngine.getAll();         
   }

   public void update() {
	   APEngine.step();
	   rotatingRect.setRotation(rotatingRect.getRotation() + (float)0.03);
//      world.step(   timeStep  , iterations);
//       postInvalidate();
       invalidate();
   }  
   @Override

   protected void onDraw(Canvas canvas) {

	   APEngine.setDefaultContainer(canvas);
		for (int i = 0; i < paintQueue.size(); i++) {
			//TG TODO need to write code that determined the type of objects and sets their method. 
			if (paintQueue.get(i) instanceof RectangleParticle)  
				((RectangleParticle)paintQueue.get(i)).paint();
			else if (paintQueue.get(i) instanceof CircleParticle) 
				((CircleParticle)paintQueue.get(i)).paint();
			else if (paintQueue.get(i) instanceof SpringConstraint) 
				((SpringConstraint)paintQueue.get(i)).paint();				
		}
    }
   
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent msg) {
//		wheelParticleA.setAngularVelocity(keySpeed);
//		wheelParticleB.setAngularVelocity(keySpeed);
       if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			wheelParticleA.setAngularVelocity(-keySpeed);
			wheelParticleB.setAngularVelocity(-keySpeed);
           return (true);
       }

       if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			wheelParticleA.setAngularVelocity(keySpeed);
			wheelParticleB.setAngularVelocity(keySpeed);
           return (true);
       }

       return super.onKeyDown(keyCode, msg); 
   }
   
   @Override
   public boolean onTrackballEvent  (MotionEvent event){
	   if (event.getAction()==MotionEvent.ACTION_MOVE)
	   {
		 wheelParticleA.setAngularVelocity(keySpeed);
         wheelParticleB.setAngularVelocity(keySpeed);
		   
	   }
	   return super.onTrackballEvent(event);
   }

}
